package cn.yunhe.controller;

import cn.yunhe.entity.*;
import cn.yunhe.service.ExamPaperService;
import cn.yunhe.service.ExamService;
import cn.yunhe.service.QuestionService;
import cn.yunhe.service.serviceImpl.ExamserviceImpl;
import cn.yunhe.service.serviceImpl.QuestionServiceImpl;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/ExamPage")
public class ExamPaperController {

    @Resource
    private ExamPaperService examPaperService;

    @Resource
    private QuestionServiceImpl questionService;

    @Resource
    private ExamserviceImpl examService;

    /**
     * 根据知识点分类分页模糊查询试卷
     *
     * @param pageSize  页大小
     * @param pageNum   当前页
     * @param examPaper 考试相关信息
     * @return
     * @author guohanyu
     */
    @RequestMapping("/getExamPaperList")
    public Map queryUserByPage(int pageSize, int pageNum, ExamPaper examPaper, Integer fieldId, Integer pointId) {
        Map map = new HashMap();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        //封装数据
        map.put("fieldId", fieldId);
        map.put("pointId", pointId);
        map.put("examPaper", examPaper);
        Page page = examPaperService.getPapersByFieldAndPointAndName(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());

        return map;

    }

    /**
     * 添加试卷
     *
     * @param contentKnowledgePoint 添加的知识点
     * @param examPaperName         试卷名称
     * @param passPoint             及格分
     * @param duration              考试时间
     * @param questionCount         多少试题
     * @param questionPoint         每道多少分
     * @param totalPoint            总分
     * @param field                 所属科目
     * @param pointId               所属分类
     * @return
     */
    @RequestMapping("/addExamPaper")
    public ModelAndView addExamPaper(HttpSession session, Integer examPaperType, String contentKnowledgePoint, String examPaperName, Integer passPoint, Integer duration, Integer questionCount, Integer questionPoint, Integer totalPoint, Integer field, Integer pointId,Integer judgmentQuestionCount,Integer judgmentQuestionPoint ){
        //创建Map封装数据
        Map<String,Object> map = new HashMap<String, Object>();
        //创建ExamPaper对象封装数据
        ExamPaper examPaper = new ExamPaper();
        examPaper.setName(examPaperName);
        examPaper.setPassPoint(passPoint);
        examPaper.setDuration(duration);
        examPaper.setTotalPoint(totalPoint);
        examPaper.setField(new Field(field));
        examPaper.setPoint(new Point(pointId));
        examPaper.setPaperType(examPaperType);
        examPaper.setCreateTime(new Date());
        examPaper.setQuestionCount(questionCount);
        examPaper.setJudgmentQuestionCount(judgmentQuestionCount);
        examPaper.setTotalQuestionCount(questionCount+judgmentQuestionCount);
        examPaper.setChooseQuestionTotalPoint(questionCount*questionPoint);
        examPaper.setJudgmentQuestionTotalPoint(judgmentQuestionCount*judgmentQuestionPoint);
     /*   User user = (User)session.getAttribute("Curr_User");
        examPaper.setCreator(user.getUserName());*/
        examPaper.setStatus(0);

        //知识点数组数据处理
        List<String> pointListString = Arrays.asList(contentKnowledgePoint.split(","));
        List<Integer> pointList = new ArrayList<>();

        for (String pl : pointListString) {
            pointList.add(Integer.parseInt(pl));
            System.out.println(pl);
        }

        //封装入map传入后台
        map.put("examPaper",examPaper);
        map.put("questionCount",questionCount);
        map.put("questionPoint",questionPoint);
        map.put("judgmentQuestionCount",judgmentQuestionCount);
        map.put("judgmentQuestionPoint",judgmentQuestionPoint);
        map.put("pointList",pointList);

        //执行新增操作
        examPaperService.addExamPaper(map);

        return new ModelAndView();
    }

    /**
     * 访问试卷详情页面
     *
     * @param examPaperId
     * @return
     */
    @RequestMapping("/toShowExamPaper")
    public ModelAndView toShowExamPaper(Integer examId, Integer examPaperId) throws ParseException {
        ModelAndView mv = new ModelAndView("showExamPaper2");
        ExamPaper examPaper = examPaperService.getExamPaperById(examPaperId);
        Map<String, Object> mapInfo = new HashMap<String, Object>();
        mapInfo.put("examId", examId);
        mapInfo.put("examPaper", examPaper);
        mv.addObject("mapInfo", mapInfo);
        return mv;
    }

    /**
     * 根据试卷Id获取试题,删选后置于试卷模板
     * @param examPaperId
     * @return
     * guohanyu
     */
    @RequestMapping("/getExamPaperAndItsQuestions")
    public Map<String, Object> getExamPaperAndItsQuestions(Integer examPaperId){
        Map<String,Object> map = new HashMap<>();
        List<Question> allQuestionList = examPaperService.getQuestionsByExamPaperId(examPaperId);
        List<Question> questionList = new ArrayList<>();
        List<Question> judgmentQuestionList = new ArrayList<>();
        //筛选出选择题列表
        for (Question aql:allQuestionList){
            if (aql.getQuestionType().getId()==1){
                questionList.add(aql);
            }
        }
        //筛选出判断题
        for (Question aql:allQuestionList){
            if (aql.getQuestionType().getId()==3){
                judgmentQuestionList.add(aql);
            }
        }
        //封装题类型
        map.put("questionList",questionList);
        map.put("judgmentQuestionList",judgmentQuestionList);
        return map;
      /*  List<Question> questionList = examPaperService.getQuestionsByExamPaperId(examPaperId);
        return questionList;*/
    }

    @RequestMapping("/subExamPage")
    public Map subExamPage(HttpSession session,String questions, String judgments, Integer examId, Integer examPaperId) {
        //得到 对的 选择题 数量
        int trueQues = questionService.getTrueCount(questions);
        //得到 对的 判断题 数量
        int trueJudg = questionService.getTrueCount(judgments);

        ExamPaper examPaper = examPaperService.getExamPaperById(examPaperId);
        //单选题数量
        int quesCount = examPaper.getQuestionCount();
        //判断题数量
        int judgCount = examPaper.getJudgmentQuestionCount();
        //单选题得分
        int quesPoint = 0;
        if(quesCount > 0){
            quesPoint = (examPaper.getChooseQuestionTotalPoint() / quesCount) * trueQues;
        }
        //判断题得分
        int judgPoint = 0;
        if(judgCount > 0){
            judgPoint = (examPaper.getJudgmentQuestionTotalPoint() / judgCount) * trueJudg;
        }
        Exam exam = examService.getExamById(examId);
        User user = (User) session.getAttribute("Curr_User");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userId", user.getUserId());
        map.put("totalPoint", examPaper.getTotalPoint());
        map.put("point", quesPoint + judgPoint);
        map.put("startTime", exam.getStartTime());
        map.put("examId", examId);
        map.put("examPaperId", examPaperId);
        Map<String ,Object>map2 = new HashMap<String, Object>();
        if(exam.getExamType() == 1){
            examService.updUser2Exam(map);
            map2.put("count", examPaperService.addUserExamHistory(map));
            map2.put("type", 1);
            return map2;
        }else {
            map2.put("point", quesPoint + judgPoint);
            map2.put("type", 2);
            return map2;
        }
    }

    public QuestionServiceImpl getQuestionService() {
        return questionService;
    }

    public void setQuestionService(QuestionServiceImpl questionService) {
        this.questionService = questionService;
    }

    public ExamPaperService getExamPaperService() {
        return examPaperService;
    }

    public void setExamPaperService(ExamPaperService examPaperService) {
        this.examPaperService = examPaperService;
    }

    public ExamserviceImpl getExamService() {
        return examService;
    }

    public void setExamService(ExamserviceImpl examService) {
        this.examService = examService;
    }
}